<?php

namespace MyLogger\Handlers;

use MyLogger\Logger;
use MyLogger\Formatters\FormatterInterface;

class BrowserConsoleHandler implements HandlerInterface
{
    protected $level;
    protected $formatter;

    public function __construct(
        int $level = Logger::DEBUG,
        FormatterInterface $formatter = null
    ) {
        $this->level = $level;
        $this->formatter = $formatter ?: new \MyLogger\Formatters\HtmlFormatter();
    }

    public function handle(array $record): bool
    {
        if ($record['level'] < $this->level) {
            return false;
        }
        
        $formatted = $this->formatter->format($record);
        echo $formatted;
        
        return true;
    }
}